package com.zzj.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.zzj.entity.File;
import com.zzj.entity.Folder;
import com.zzj.mapper.FileMapper;
import com.zzj.service.IFileService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zzj.utils.FileUtil;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

/**
 * <p>
 * 文件表 服务实现类
 * </p>
 *
 * @author zzj
 * @since 2023-11-14
 */
@Service
public class FileServiceImpl extends ServiceImpl<FileMapper, File> implements IFileService {
    @Resource
    private FileUtil fileUtil;

    @Override
    public File create(String creatorId, String name, String size, String path, String parentId) {
        File file = new File();
        file.setCreatorId(creatorId);
        file.setName(name);
        file.setSize(size);
        file.setPath(path);
        file.setParentId(parentId);
        if (this.save(file)) {
            return file;
        }
        return null;
    }

    @Override
    public boolean checkExist(String creatorId, String name, String parentId) {
        File one = this.getOne(Wrappers.<File>lambdaQuery()
                .eq(File::getParentId, parentId)
                .eq(File::getName, name)
                .eq(File::getCreatorId, creatorId), false);
        return one != null;
    }

    @Override
    public List<File> queryList(String creatorId, String parentId) {
        return this.list(Wrappers.<File>lambdaQuery()
                .eq(File::getParentId, parentId)
                .eq(File::getCreatorId, creatorId)
                .orderByDesc(File::getCreatedAt));
    }

    @Override
    public List<File> queryList(String creatorId, String parentId, String searchKey) {
        return this.list(Wrappers.<File>lambdaQuery()
                .eq(File::getCreatorId, creatorId)
                .eq(File::getParentId, parentId)
                .like(File::getName, searchKey)
                .orderByDesc(File::getCreatedAt));
    }

    @Override
    public List<File> queryList(String creatorId, String searchKey, boolean f) {
        return this.list(Wrappers.<File>lambdaQuery()
                .eq(File::getCreatorId, creatorId)
                .like(File::getName, searchKey)
                .orderByDesc(File::getCreatedAt));
    }

    @Override
    public boolean remove(String id) {
        //数据库查询存在 得到路径
        File file = this.getById(id);
        if (file != null) {
            //删除文件资源
            boolean b = fileUtil.removeFile(file.getPath());
            //删除数据库记录
            if (b) {
                return this.removeById(id);
            }
        }
        return false;
    }

    @Override
    public boolean removes(String parentId) {
        List<File> list = this.list(Wrappers.<File>lambdaQuery()
                .eq(File::getParentId, parentId));
        boolean res = true;
        for (File file : list) {
            res = remove(String.valueOf(file.getId()));
            if (!res) {
                return false;
            }
        }
        return res;
    }

    @Override
    public boolean updateName(String id, String parentId, String name) {
        File one = this.getOne(Wrappers.<File>lambdaQuery()
                .eq(File::getParentId, parentId)
                .eq(File::getName, name), false);
        if(Objects.equals(parentId, "")){
            one = null;
        }
        if (one == null) {
            File file = new File();
            file.setId(Integer.valueOf(id));
            file.setName(name);
            return this.updateById(file);
        }
        return false;

    }

    @Override
    public File getById(String id) {
        return this.getOne(Wrappers.<File>lambdaQuery()
                .eq(File::getId, id), false);
    }

    @Override
    public List<File> getFileList(String parentId) {
        return this.list(Wrappers.<File>lambdaQuery()
                .eq(File::getParentId, parentId));

    }
}
